package com.example.opengldemo.simple13_01;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorEventListener2;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.opengldemo.R;

public class ORIENTATIONActivity extends AppCompatActivity {
    TextView text;
    TextView textX;
    TextView textY;
    TextView textZ;
    SensorManager manager;
    float []vlAccelerometer=new float[3];
    float []vlManager=new float[3];


    private SensorEventListener myAccelerometerListener =
            new SensorEventListener(){
                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy){}
                @Override
                public void onSensorChanged(SensorEvent event){
                    vlAccelerometer=event.values;
                    float[] R=new float[9];
                    SensorManager.getRotationMatrix
                            (
                                    R,
                                    null,
                                    vlAccelerometer,
                                    vlManager
                            );
                    float[] Values=new float[3];
                    SensorManager.getOrientation(R, Values);
                    textX.setText(  "Yaw 轴的旋转角度 :"+Values[0]);
                    textY.setText("Pitch 轴的旋转角度 :"+Values[1]);
                    textZ.setText( "Roll 轴的旋转角度 :"+Values[2]);
                }
            };
    private SensorEventListener myMagneticListener=new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            vlManager=event.values;
            float[] R=new float[9];
            SensorManager.getRotationMatrix
                    (
                            R,
                            null,
                            vlAccelerometer,
                            vlManager
                    );
            float[] Values=new float[3];
            SensorManager.getOrientation(R, Values);
            textX.setText(  "Yaw 轴的旋转角度 :"+Values[0]);
            textY.setText("Pitch 轴的旋转角度 :"+Values[1]);
            textZ.setText( "Roll 轴的旋转角度 :"+Values[2]);
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
    Sensor myAccelerometer ;
    Sensor myMagnetic ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        text = findViewById(R.id.text);
        textX = findViewById(R.id.textX);
        textY = findViewById(R.id.textY);
        textZ = findViewById(R.id.textZ);

        manager = (SensorManager) getSystemService(SENSOR_SERVICE);

    }

    @Override
    protected void onStart() {
        super.onStart();
        myAccelerometer=manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        myMagnetic=manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(myAccelerometerListener != null && myAccelerometer != null)
            manager.registerListener(myAccelerometerListener , myAccelerometer ,  SensorManager.SENSOR_DELAY_NORMAL) ;
        if(myMagneticListener != null && myMagnetic != null)
            manager.registerListener(myMagneticListener , myMagnetic ,  SensorManager.SENSOR_DELAY_NORMAL) ;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(myAccelerometerListener != null )
            manager.unregisterListener(myAccelerometerListener );
        if(myMagneticListener != null )
            manager.unregisterListener(myMagneticListener ); ;

    }

}
